package org.kev.transit.data;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.httpclient.HttpException;
import org.json.JSONException;
import org.kev.transit.TransitProvider;
import org.kev.transit.util.JsonHelper;
import org.kev.transit.util.WebHelper;
import org.kev.transit.worker.RouteTypeUpdateWorker;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;


public class RouteType implements BaseColumns{
	/**
	* The default sort order for this table
	*/
	public static final String DEFAULT_SORT_ORDER = "title DESC";
	public static final String TITLE = "title";
	public static final String ROUTE_TYPE_ID = "route_type_id";
	public static final String CREATE_DATE = "create_date";
	public static final String TABLE_NAME = "route_type";
    public static final Uri CONTENT_URI = Uri.parse("content://"+TransitProvider.AUTH+"/"+TABLE_NAME);
	private static final String TAG = "RouteType";
    public String id = null;
    public String title=null;
    public long createDate=0;
    
    /**
     *  get a list of rout types form the transit provider
     * @param parent
     * @return list of RouteTypes
     */
    public static final ArrayList <RouteType> getData(Activity parent){
    	Log.d(TAG,">>> getData");
    	Cursor cursor = null;
    	ArrayList <RouteType> list = new ArrayList <RouteType>();
    	boolean hasData=false;
		try{
				
				cursor = parent.managedQuery(RouteType.CONTENT_URI, 
						new String[] {RouteType.ROUTE_TYPE_ID,RouteType.TITLE}, null, null);
				Log.d(TAG,"rturn rows="+cursor.count());
				while(cursor.next()){
					hasData=true;
					String id=cursor.getString(0);
					String name=cursor.getString(1);
					RouteType rtype=new RouteType();
					rtype.id=id;
					rtype.title=name;
					list.add(rtype);
				}
				
		}finally{
			if (cursor!=null)
				cursor.close();
		}
		if(!hasData){
			try {
				list=retrieveDataFromWeb(parent);
			} catch (Exception e) {
				Log.e(TAG,"error getting  web data!",e);
			} 
		}
		Log.d(TAG,">>> getData");
		if(list==null);
			
    	return list;
    }
	/**
	 * calls web service 
	 * Returning a list of data objects.  It will then try to call
	 * a background worker thread to cache the data in the local DB.
	 * @param parent calling reference
	 * @param rout type id
	 * @return list of Direction objects
	 * @throws HttpException
	 * @throws IOException
	 * @throws JSONException
	 */
	private static ArrayList <RouteType> retrieveDataFromWeb(Activity parent) throws HttpException, IOException, JSONException{
		Log.d(TAG,"did not find any data in tables get it from the web");
		String jsonText=null;
		jsonText=WebHelper.getRouteType();
		ArrayList <RouteType> list= new ArrayList <RouteType>();
		list= JsonHelper.getRouteTypes(jsonText);
		Log.d(TAG,"Retrieved data from web="+list.size());
		/*for(int i=0;i<list.size();i++){
			Stop stop = list.get(i);
			Log.d(TAG,"stop="+stop.toXml());
		}*/

		//update db in background
		RouteTypeUpdateWorker worker = new RouteTypeUpdateWorker(parent, list);
		Thread t = new Thread(worker);
		t.start();
		//worker.run();
		return list;
	}   
    @Override
    /**
     * 
     */
    public String toString(){
    	return title;
    }
	/**
	 * dumps current instance values to a string 
	 * in a XML format
	 * @return XML
	 */
	public String toXml(){
		return
		"<"+TABLE_NAME+">\n"+
		"\t<"+Route._ID+">"+id+"</"+Route._ID+">\n"+
		"\t<"+Route.TITLE+">"+title+"<"+Route.TITLE+">\n"+
		"\t<"+Route.CREATE_DATE+">"+createDate+"</"+Route.CREATE_DATE+">\n"+
		"</"+TABLE_NAME+">\n" ;
	}
}
